angular.module('starter.services', [])


    .service('dataService', dataService)
    .service('sessionService', sessionService)
    .service('storageService', storageService)
    .service("openIdMessageService", openIdMessageService)
    .service('wxOpenIdService',wxOpenIdService);

function sessionService($window) {

    return {
        set: set,
        get: get,
        setObject: setObject,
        getObject: getObject
    };

    function set(key, value) {
        $window.sessionStorage[key] = value;
    }

    function get(key) {
        return $window.sessionStorage[key] || '';
    }

    function setObject(key, value) {
        $window.sessionStorage[key] = JSON.stringify(value);
    }

    function getObject(key) {
        return JSON.parse($window.sessionStorage[key] || '{}');
    }
}


function storageService($window) {

    return {
        set: set,
        get: get,
        setObject: setObject,
        getObject: getObject
    };

    function set(key, value) {
        $window.localStorage[key] = value;
    }

    function get(key) {
        return $window.localStorage[key] || '';
    }

    function setObject(key, value) {
        $window.localStorage[key] = JSON.stringify(value);
    }

    function getObject(key) {
        return JSON.parse($window.localStorage[key] || '{}');
    }

}

function dataService($http, $q, exception) {

    return {
        get: get,
        post: post,
        put: put,
        del: del
    };

    function success(resp) {
        return $q.resolve(resp.data);
    }

    function failure(error) {
        return $q.reject(error);
    }

    function get(address, qParams, shouldCache) {

        var config = {
            method: 'GET',
            url: address,
            params: qParams || null,
            cache: shouldCache || false
        };
        return $http(config).then(success).catch(failure);
    }

    function post(address, data) {
        var config = {
            method: 'POST',
            url: address,
            data: data
        };
        return $http(config).then(success).catch(failure);
    }

    function put(address, data) {
        return $http.put(address, data).then(success).catch(failure);

    }

    function del(address,data) {
        return $http.delete(address).then(success).catch(failure);
    }

}

function wxOpenIdService($location,$q,$http) {
    return {
        getCode:getCode,
        getOpenId:getOpenId
    }
    function getCode() {
        window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4a3fd351428d5e97&redirect_uri="+$location.absUrl().split('?')[0]+"&response_type=code&scope=snsapi_base&state="+$location.path()+"#wechat_redirect";
    }
    function getOpenId(code) {
        var deferred = $q.defer();
        $http.get(API("huanbi/security/getToken?code="+code))
            .then(function(result){
                if(result.openId==''){
                    deferred.reject();
                }else{
                    deferred.resolve(result);
                }
            }).catch(function(error){
            deferred.reject();
        });
        return  deferred.promise;
    }
}

function openIdMessageService($q,$http,$rootScope) {
    return {
        getMessage: getMessage,
        findBlack:findBlack

    }
    function getMessage() {
        var deferred = $q.defer();
        $http.get(API('huanbi/findUser?openId=') + $rootScope.openId)
            .then(function (result) {
                if (result.data.successful) {
                    deferred.resolve(result.data);
                } else {
                    deferred.reject();
                }
            }).catch(function (error) {
            deferred.reject();
        });
        return deferred.promise;
    };
    function findBlack(openId) {
        var deferred = $q.defer();
        $http.get(API('huanbi/blankList/findBlankList?id=') + openId)
            .then(function (result) {
                if (result.data) {
                    deferred.resolve(result.data);
                } else {
                    deferred.reject();
                }
            }).catch(function (error) {
            deferred.reject();
        });
        return deferred.promise;
    }
};